const express = require('express');
const proxy = require('http-proxy-middleware');
const app = express();

const directory = process.argv[2];
const port = process.argv[3];

console.log(process.argv);

app.use(function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});

app.use(function (req, res, next) {
  console.log(req.originalUrl);
  next();
});

// app.use(express.static(directory));

app.use(
  proxy('/isim-backend', {
    target: 'http://192.168.1.249/bdms',
    // target: 'http://192.168.1.226:8080',
    // target: 'http://192.168.1.227:8080',
    // target: 'http://39.96.189.123:80',
    // target: "http://sz-tct.info:2278",
    changeOrigin: true,
  }),
);

app.use(
  proxy('/sms-user-backend', {
    target: 'http://192.168.1.249/bdms',
    // target: 'http://192.168.1.226:8080',
    // target: 'http://192.168.1.227:8080',
    // target: 'http://39.96.189.123:80',
    // target: "http://sz-tct.info:2278",
    changeOrigin: true,
  }),
);

app.use('/', express.static(directory));

// app.use("/img", express.static(directory));
// app.use("/fonts", express.static(directory));
// app.use("/js", express.static(directory));
// app.use("/static", express.static(directory));

app.listen(port, () =>
  console.log(`HTTP server listening on port ${port}, exposing directory ${directory}.`),
);
